function returnIndex(){
    window.location.href = 'index.html';
}

document.addEventListener('DOMContentLoaded', function(){
    const registerForm = document.getElementById("registerForm");
    if (registerForm) {
        registerForm.addEventListener("submit", function (event) {
            event.preventDefault();

            const formData = new FormData(this);
            const name = formData.get("name").trim();
            const pwd = formData.get("pwd").trim();
            const confirmPwd = formData.get("confirmPwd").trim();

            if (pwd !== confirmPwd) {
                alert("两次密码不一致！");
                return;
            }

            // 构造 URL 编码的参数
            const formBody = new URLSearchParams();
            formBody.append("name", name);
            formBody.append("pwd", pwd);

            fetch("http://localhost:8089/api/admin/register", {
                method: "POST",
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                body: formBody
            })
                .then(res => res.text())
                .then(result => {
                    if (result === "注册成功") {
                        alert("注册成功！");
                        window.location.href = "login.html";
                    } else {
                        alert("注册失败：" + result);
                    }
                })
                .catch(error => {
                    console.error("注册请求出错：", error);
                });
        });
    } else {
        console.error("Register form not found");
    }
});